package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Transactional(readOnly = true)
    @Override
    public PageInfo<User> findByPage(int page, int pageSize) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //查询所有
        List<User> list = userDao.findAll();
        //封装分页结果
        return new PageInfo<>(list);
    }

    @Transactional(readOnly=true)
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Transactional(readOnly=true)
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public void save(User user) {
          userDao.save(user);
    }

    @Override
    public void update(User user) {
         userDao.update(user);
    }

    @Override
    public void delete(Integer id) {
          userDao.delete(id);
    }

    @Transactional(readOnly=true)
    @Override
    public User login(String username, String password) {
        return userDao.findUserByUsernameAndPassword(username,password);
    }
}
